// pages/part2/multiple.js
const db=wx.cloud.database()
const question_db=db.collection('question')
import Toast from '@vant/weapp/toast/toast';
Page({
    data: {
      total: 0,
      answer: "A",
      current: 1, //从第一题开始
      isSelect: false,
      questionResult: [],
      titles:[],
      xz:[],
    },
  
  onShow(){
   
    
  },
    onLoad() {
      var that=this
      that.setData({
        id:that.options.id,
      })
      var version=wx.getStorageSync('bb')
      question_db.where({"_id":version})
    .get()
    .then(res=>{
      this.setData({
        titles:res.data[0].question[0]['regular']
      })
      var subject = this.data.titles[0]
      console.log("subject", subject)
      this.setData({ 
        subject: subject,
        total: this.data.titles.length
      })
      if(this.data.id){
        // that.setData({
          // current:parseInt(this.data.id),
          // subject:this.data.titles[this.data.id-1],
          console.log('xxxx')
          this.setData({
            current:parseInt(this.data.id),
          subject:this.data.titles[this.data.id-1],
          })
          var multiple=wx.getStorageSync('regular')
          console.log(multiple)
          this.setData({
            userxz:multiple[this.data.current-1]
          })
        // })
        // console.log(this.data.subject)
      }
  })
  
  
  var multiple=wx.getStorageSync('regular')
  console.log(multiple)
  this.setData({
    userxz:multiple[this.data.current-1]
  })

     
    },
    onPageScroll: function (e) {
      console.log(e)
      if (e.scrollTop > 100) {
       this.setData({
        floorstatus: true
       });
      } else {
       this.setData({
        floorstatus: false
       });
      }
     },
    next(){
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
         scrollTop: 0
        })
       } 
      this.setData({
        userxz:'',
      })
      if (this.data.current < this.data.total) {
        this.setData({
          current: this.data.current + 1,
          subject: this.data.titles[this.data.current],
          // userxz:this.data.getQR[this.data.current].select
        })
      
      
      var multiple=wx.getStorageSync('regular')
      console.log(multiple)
      this.setData({
        userxz:multiple[this.data.current-1]
      })
      }
      else if(this.data.current == this.data.total){
        const toast = Toast.loading({
          duration: 0, // 持续展示 toast
          forbidClick: true,
          message: '进入多选题!',
          selector: '#custom-selector',
        });
        let second = 2;
        const timer = setInterval(() => {
          second--;
          if (second) {
            toast.setData({
              message: ` ${second} 秒后进入`,
            });
          } else {
            clearInterval(timer);
            Toast.clear();
            wx.redirectTo({
              url: '../part2/multiple',
            })
          }
        }, 1000);
}
    },
    last(){
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
         scrollTop: 0
        })
       } 
      this.setData({
        userxz:'',
      })
        if (this.data.current > 1) {
          this.setData({
            current: this.data.current -1,
            
          })
          this.setData({
            subject: this.data.titles[this.data.current-1],
          })
        }
        var multiple=wx.getStorageSync('regular')
        console.log(multiple)
        this.setData({
          userxz:multiple[this.data.current-1]
        })
    },
    onclick(e){
      let u = e.target.dataset.select
      
      
      this.setData({
        userxz:u
      })
      this.data.xz[this.data.current-1]=u
      wx.setStorageSync('regular', this.data.xz)
      
    },
    
    submit(e) {
      if(!this.data.getQR){
      console.log(e)
      let userSelcet = e.target.dataset.select
      if (userSelcet !== undefined) {
        this.data.questionResult.push({
          "questionId": this.data.current,
          "select": userSelcet
        }) // 记录
        // 不是最后一题，current+1,并进行下一题
    
       
      
      //  this.setData({
      //    userxz:getQR[this.data.current]
      //  })
        if (this.data.current < this.data.total) {
          this.setData({
            current: this.data.current + 1,
            subject: this.data.titles[this.data.current]
          })
        }
        // 如果已经到了最后一题
        if (this.data.current === this.data.total) {
          wx.showToast({
            icon: 'none',
            title: '已经最后一道题啦',
          })
        }
        // 答完最后一道题
        if (this.data.current === this.data.total && this.data.questionResult.length === this.data.total) {
          console.log("恭喜你全部答完")
          console.log(this.data.questionResult)
          wx.navigateTo({
            url: '../part2/review',
          })
          wx.setStorageSync('QR', this.data.questionResult)
        }
      }
    }
    else{
  
      console.log(e)
      let userSelcet = e.target.dataset.select
      if (userSelcet !== undefined) {
        this.data.getQR[this.data.current-1]={
          "questionId": this.data.current,
          "select": userSelcet
        }
        console.log(this.data.getQR)
        wx.setStorageSync('QR', this.data.getQR)
        var getQR=wx.getStorageSync('QR')
        this.setData({
          getQR:getQR,
          userxz:this.data.getQR[this.data.current].select
        })
        if (this.data.current < this.data.total) {
          this.setData({
            current: this.data.current + 1,
            subject: this.data.titles[this.data.current]
          })
        }
      }
    }
    },
  
  })